// src/pages/catering/goodsList/hotelGoodsList/date/date.js
import getIn from '../../../../../../yali-sdk/utils/getIn';
import { notify } from '../../../../../../yali-sdk/utils/NotifyRegistryCenter';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: [],
    weeks: [],
    newSelectDate: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const selectDate = JSON.parse(options.selectDate);
    this.dateData(selectDate);
  },
  dateData(selectDate) {
    let dataAll = []//总日历数据
    let dataAll2 = []//总日历数据
    let dataMonth = []//月日历数据
    let date = new Date//当前日期
    let year = date.getFullYear()//当前年
    let week = date.getDay();//当天星期几
    let weeks = []
    let month = date.getMonth() + 1//当前月份
    let day = date.getDate()//当天
    let daysCount = 90//一共显示多少天
    let dayscNow = 0//计数器
    let nowMonthList = []//本年剩月份
    for (let i = month; i < 13; i++) {
      nowMonthList.push(i)
    }
    let yearList = [year]//年份最大可能
    for (let i = 0; i < daysCount / 365 + 2; i++) {
      yearList.push(year + i + 1)
    }
    for (let i = 0; i < yearList.length; i++) {//遍历年
      let mList
      if (yearList[i] == year) {//判断当前年份
        mList = nowMonthList
      } else {
        mList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
      }
      for (let j = 0; j < mList.length; j++) {//循环月份
        dataMonth = []
        let t_days = [31, 28 + this.leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        let t_days_thisYear = []
        if (yearList[i] == year) {
          for (let m = 0; m < nowMonthList.length; m++) {
            t_days_thisYear.push(t_days[mList[m] - 1])
          }
          t_days = t_days_thisYear
        } else {
          t_days = [31, 28 + this.leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        }
        for (let k = 0; k < t_days[j]; k++) {//循环每天
          dayscNow++
          let nowData
          if (dayscNow < daysCount) {//如果计数器没满
            let days = k + 1
            if (days < 10) {
              days = "0" + days
            }
            if (yearList[i] == year && mList[j] == month) {//判断当年当月
              if (k + 1 >= day) {
                nowData = {
                  year: yearList[i],
                  month: mList[j],
                  day: k + 1,
                  ...this.onSelect(selectDate, yearList[i], mList[j], k + 1)
                }
                dataMonth.push(nowData)
                if (k + 1 == day) {
                  let date = new Date(yearList[i] + "-" + mList[j] + "-" + (k + 1))
                  let weekss = date.getDay()//获取每个月第一天是周几
                  weeks.push(weekss)
                }
              }
            } else {//其他情况
              nowData = {//组装自己需要的数据
                year: yearList[i],
                month: mList[j],
                day: k + 1,
                ...this.onSelect(selectDate, yearList[i], mList[j], k + 1)
              }
              dataMonth.push(nowData)
              if (k == 0) {
                let date = new Date(yearList[i] + "-" + mList[j] + "-" + k + 1)
                let weekss = date.getDay()//获取每个月第一天是周几
                weeks.push(weekss)
              }
            }
          } else {
            break
          }
        }
        dataAll.push(dataMonth)
      }
    }
    for (let i = 0; i < dataAll.length; i++) {
      if (dataAll[i].length != 0) {
        dataAll2.push(dataAll[i]);
      }
    };
    this.setData({
      date: dataAll2,
      weeks: weeks
    })
  },
  //判断是否闰年
  leapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
      return (true);
    } else { return (false); }
  },
  // 判断是否传当前选择日期
  onSelect(selectDate, year, month, day) {
    let selected = 0, text = '';
    const startDate = new Date(`${selectDate[0].year}/${selectDate[0].month}/${selectDate[0].day}`);
    const endDate = selectDate[1] && new Date(`${selectDate[1].year}/${selectDate[1].month}/${selectDate[1].day}`);
    const time = new Date(`${year}/${month}/${day}`);
    if (time.getTime() == startDate.getTime()) {
      selected = 1;
      text = '入住';
    } else if (selectDate[1] && time.getTime() == endDate.getTime()) {
      selected = 1;
      text = '离店';
    } else if (selectDate[1] && startDate.getTime() < time.getTime() && endDate.getTime() > time.getTime()) {
      selected = 1;
      text = '';
    } else {
      selected = 0;
      text = '';
    }
    return { selected, text }
  },
  selectday(e) {
    const item = getIn(e.currentTarget, ['dataset', 'item']);
    let { newSelectDate, date } = this.data;
    let newDate = [...date];
    if (newSelectDate.length == 1) {
      const startDate = new Date(`${newSelectDate[0].year}/${newSelectDate[0].month}/${newSelectDate[0].day}`)
      const endDate = new Date(`${item.year}/${item.month}/${item.day}`);
      if (Date.parse(startDate) < Date.parse(endDate)) {
        newSelectDate.push(item);
      } else {
        newSelectDate = [item]
      }
    } else {
      newSelectDate = [item]
    }
    newDate = newDate && newDate.map((monthList) => {
      monthList.map((x) => {
        const param = this.onSelect(newSelectDate, x.year, x.month, x.day);
        x.selected = param.selected;
        x.text = param.text;
        return x
      });
      return monthList;
    });
    this.setData({ newSelectDate, date: newDate });
    if (newSelectDate.length == 2) {
      const day1 = new Date(`${newSelectDate[0].year}/${newSelectDate[0].month}/${newSelectDate[0].day}`);
      const day2 = new Date(`${newSelectDate[1].year}/${newSelectDate[1].month}/${newSelectDate[1].day}`);
      notify && notify('UpdateDate', { day1, day2 });
      global.navigateHelper && global.navigateHelper.navigateBack();
    }
  }
})